package cn.xyafu.lost.activity;

import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.xyafu.lost.R;
import cn.xyafu.lost.adapter.ListViewBodyAdapter;
import cn.xyafu.lost.pojo.Lost;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class UserLostActivity extends AppCompatActivity {

    private Button back;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_lost);

        back = findViewById(R.id.btn_back);
        listView = findViewById(R.id.lv_user_lost);

        Intent intent = getIntent();
        String uid = intent.getStringExtra("uid");

        //  发起网络请求
        // (1). 创建OkHttpCLient对象
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(5, TimeUnit.SECONDS);
        builder.readTimeout(5, TimeUnit.SECONDS);
        builder.writeTimeout(5, TimeUnit.SECONDS);
        OkHttpClient client = builder.build();
        // (2). 创建Request对象
        Request request = new Request.Builder().url("http://localhost/Lost_ssm/android/userLost?uid="+uid).get().build();
        // (3). 创建Call对象
        Call call = client.newCall(request);
        // (4). 将请求添加到队列中
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("请求失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求成功");
                // 1. 将json数据转化为model数据
                String jsonString = response.body().string();
                final List<Lost> losts = JSON.parseArray(jsonString, Lost.class);
                System.out.println("pageInfo:"+losts);

                // 2 更新界面
                back.post(new Runnable() { // 由于在子线程中,需要通过post来更新界面
                    @Override
                    public void run() {
                        // 3. 将数据传递给适配器,然后将适配器适配给listview
                        final ListViewBodyAdapter bodyAdapter = new ListViewBodyAdapter(UserLostActivity.this,losts);
                        listView.setAdapter(bodyAdapter);
                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                TextView textView = view.findViewById(R.id.item_lid_view);
                                String lid = textView.getText().toString().trim();
                                Intent intent = new Intent(UserLostActivity.this, LostDetailActivity.class);
                                intent.putExtra("lid",lid);
                                startActivityForResult(intent,2);
                            }
                        });
                    }
                });

            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

}
